package com.rongc.client.freight.business.supply.view.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.business.carrier.view.fragment.ConveyanceSupplyFragment;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.request.api.SupplyList2MatchApi;
import com.rongc.client.freight.business.waybill.request.api.WaybillCreateApi;
import com.rongc.client.freight.business.waybill.view.activity.WayBillNewActivity;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.flyco.dialog.widget.NormalDialog.STYLE_TWO;
import static com.rongc.client.freight.business.supply.model.SupplyDic.StatusType.YAJIN_STATUS;

/**
 * Created by Bruce on 2017/7/7.
 */
public class SupplyList2MatchActivity extends BaseActivity {
    ConveyanceSupplyFragment fragment;
    String conveyanceId;
    String waybillId;
    int page=1;
    NormalDialog payDialog;

    public void doSearch(int page){
        this.page = page;
        SupplyList2MatchApi p = new SupplyList2MatchApi(new SupplyList2MatchApi.SupplyFindParams(),
                respWorkersListener, errorListener);
        RequestManager.getInstance().call(p);
    }

    @Override
    public void callbackFun1(Bundle arg) {
        page = arg.getInt("page",page);
        doSearch(page);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_match, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_create:
                Bundle bundle = new Bundle();
                bundle.putString("conveyanceId", conveyanceId);
                ActivityUtils.startActivity(SupplyList2MatchActivity.this, SupplyCreateActivity.class,bundle);
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    Response.Listener<JSONObject> respCreateListener = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            if (HandleCode.requestSuccess()) {
                waybillId = response.optString("id");

                Bundle bundle = new Bundle();
                bundle.putString("data",waybillId);
                bundle.putInt("status",YAJIN_STATUS);
                ActivityUtils.startActivity(SupplyList2MatchActivity.this, WayBillNewActivity.class,bundle);

//                payDialog.show();
            } else {
                UtilMethod.dismissProgressDialog(SupplyList2MatchActivity.this);
            }
        }
    };

    Response.Listener<SupplyBean[]> respWorkersListener = new Response.Listener<SupplyBean[]>() {

        @Override
        public void onResponse(SupplyBean[] response) {
            if (HandleCode.requestSuccess()) {
//                ActivityUtils.toast(HandleCode.headerMessage);
                List<SupplyBean> datas = new ArrayList<>();
                if (response != null)
                for (int i = 0;i<response.length;i++){
                    datas.add(response[i]);
                }
                fragment.setDatas(datas,page);
            } else {
                UtilMethod.dismissProgressDialog(SupplyList2MatchActivity.this);
            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(SupplyList2MatchActivity.this);
        }
    };

    @Override
    public void initParams() {
        conveyanceId = getIntent().getStringExtra("conveyanceId");
    }

    @Override
    public void initView() {
        initToolbar("我的货源");
        fragment = ConveyanceSupplyFragment.getInstance();
        fragment.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        fragment.setListener(new RecyclerBaseAdapter.OnItemClickListener<SupplyBean>() {
            @Override
            public void onItemClick(View view, int position, SupplyBean data) {
                WaybillCreateApi p = new WaybillCreateApi(new WaybillCreateApi.WaybillCreateParams(
                        conveyanceId,data.getId(),data.getFreightByType()),
                        respCreateListener, errorListener);
                RequestManager.getInstance().call(p);
            }
        });
        ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),fragment,R.id.contentFrame);
        doSearch(1);
        makePayDialog();
    }

    void makePayDialog(){
        payDialog = new NormalDialog(this);
        payDialog.content("线上支付送货物保险，为了保障您的货物安全，请线上支付。")
                .style(STYLE_TWO)
                .btnText(new String[]{"取消","支付"})
                .setOnBtnClickL(new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        payDialog.cancel();
                    }
                },new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        Bundle bundle = new Bundle();
                        bundle.putString("data",waybillId);
                        bundle.putInt("status",YAJIN_STATUS);
                        ActivityUtils.startActivity(SupplyList2MatchActivity.this, WayBillNewActivity.class,bundle);
                        payDialog.cancel();
                        finish();
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_supply_pipei;
    }
}
